<?php

//判断红包金额
if (floatval($_P['comm61'])>floatval($_P['comm62']) ) {
    return_msg('员工提成金额设置错误，右边金额必须大于等于左边金额');
}
if (floatval($_P['comm_agent1'])>floatval($_P['comm_agent2']) ) {
    return_msg('代理提成金额设置错误，右边金额必须大于等于左边金额');
}
if (floatval($_P['comm11'])>floatval($_P['comm12']) || floatval($_P['comm11_2'])>floatval($_P['comm12_2']) || floatval($_P['comm11_3'])>floatval($_P['comm12_3'])) {
    return_msg('一级分销金额设置错误，右边金额必须大于等于左边金额');
}
if (floatval($_P['comm21'])>floatval($_P['comm22']) || floatval($_P['comm21_2'])>floatval($_P['comm22_2'])) {
    return_msg('二级分销金额设置错误，右边金额必须大于等于左边金额');
}
if (floatval($_P['comm31'])>floatval($_P['comm32']) || floatval($_P['comm31_2'])>floatval($_P['comm32_2'])) {
    return_msg('三级分销金额设置错误，右边金额必须大于等于左边金额');
}
if (floatval($_P['comm41'])>floatval($_P['comm42']) || floatval($_P['comm41_2'])>floatval($_P['comm42_2']) || floatval($_P['comm41_3'])>floatval($_P['comm42_3'])) {
    return_msg('裂变红包金额设置错误，右边金额必须大于等于左边金额');
}
if (floatval($_P['comm41'])>floatval($_P['comm42']) || floatval($_P['comm41_2'])>floatval($_P['comm42_2']) || floatval($_P['comm41_3'])>floatval($_P['comm42_3'])) {
    return_msg('裂变红包金额设置错误，右边金额必须大于等于左边金额');
}


if (floatval($_P['hbopenfee1'])>floatval($_P['hbopenfee2']) || floatval($_P['hborderfee1'])>floatval($_P['hborderfee2'])) {
    return_msg('拆红包金额设置错误，右边金额必须大于等于左边金额');
}

$maxcomm1 = max(floatval($_P['comm12']),floatval($_P['comm12_2']),floatval($_P['comm12_3']));
$maxcomm2 = max(floatval($_P['comm22']),floatval($_P['comm22_2']));
$maxcomm3 = max(floatval($_P['comm32']),floatval($_P['comm32_2']));
$maxcomm4 = max(floatval($_P['comm42']),floatval($_P['comm42_2']),floatval($_P['comm42_3']));

$comm1 = $maxcomm1 + $maxcomm2 + $maxcomm3 + $maxcomm4 +floatval($_P['hbopenfee2'])+floatval($_P['hborderfee2']);
if (floatval($_P['joinfee'])>0){
    $feepay=floatval($_P['joinfee']);
}elseif (floatval($_P['groupprice'])>0){
    if ($_P['groupprice']>$_P['groupprice2'] && floatval($_P['groupprice2'])>0 && $system['bottom_jiage'] == '0'){
        $feepay=floatval($_P['groupprice2']);
    }else{
        $feepay=floatval($_P['groupprice']);
    }
}else{
    $feepay=floatval($_P['primeprice']);
}
if ($comm1>$feepay && $feepay>0){
    return_msg('所有分销总金额不能大于支付金额');
}
if ($system['bottom_jiage'] == '1' || $system['bottom_jiage'] == '2'){
    $maxcomm1 = max(floatval($_P['comm11_jiage2']),floatval($_P['comm12_jiage2']));
    $maxcomm2 = max(floatval($_P['comm21_jiage2']),floatval($_P['comm22_jiage2']));
    $maxcomm3 = max(floatval($_P['comm31_jiage2']),floatval($_P['comm32_jiage2']));
    $comm1 = $maxcomm1 + $maxcomm2 + $maxcomm3 + floatval($_P['hbopenfee2'])+floatval($_P['hborderfee2']);
    if ($comm1>=floatval($_P['groupprice2'])){
        return_msg('第二个价格的分销金额配置错误');
    }
}
if ($system['bottom_jiage'] == '2'){
    $maxcomm1 = max(floatval($_P['comm11_jiage3']),floatval($_P['comm12_jiage3']));
    $maxcomm2 = max(floatval($_P['comm21_jiage3']),floatval($_P['comm22_jiage3']));
    $maxcomm3 = max(floatval($_P['comm31_jiage3']),floatval($_P['comm32_jiage3']));
    $comm1 = $maxcomm1 + $maxcomm2 + $maxcomm3 + floatval($_P['hbopenfee2'])+floatval($_P['hborderfee2']);
    if ($comm1>=floatval($_P['groupprice3'])){
        return_msg('第三个价格的分销金额配置错误');
    }
}

if ((floatval($_P['hbopenfee2'])+floatval($_P['hborderfee2']))>floatval($_P['hbtixian'])){
    return_msg('拆红包中：【进入链接获得】+【自己报名获得】必须小于【提现限额】');
}

if(!empty($_P['comm41'])){
    if(empty($_P['comm4daynum']) || empty($_P['comm4city']) || empty($_P['comm4day']) || empty($_P['comm4hour'])){
        return_msg('开启裂变红包,每天多少个、每天预算、每小时预算、城市四个必填项目必须填!');
    }
}
if(!empty($_P['comm51'])){
    if(empty($_P['comm5daynum']) || empty($_P['comm4city']) || empty($_P['comm4day']) || empty($_P['comm4hour'])){
        return_msg('开启阅读红包,每天多少个、每天预算、每小时预算、城市四个必填项目必须填!');
    }
}
if($_G['companyname'] =='金策软件') {
    $hbtip = tomedia($_P['hbtip']);
    $fenxiaotip = tomedia($_P['fenxiaotip']);
    if (empty($hbtip)){
        $hbtip="https://css2020.yixuan.net/2020css/jince/comm_icon.png";
    }
    if (empty($fenxiaotip)){
        $fenxiaotip="https://css2020.yixuan.net/2020css/jince/comm_share.gif";
    }
}else{
    $hbtip = tomedia($_P['hbtip']);
    $fenxiaotip = tomedia($_P['fenxiaotip']);
}

$tempconfig=array(
    'iscomm'      => intval($_P['iscomm']),
    'comm_avatar_user'      => intval($_P['comm_avatar_user']),
    'auto_settlement'      => intval($_P['auto_settlement']),
    'iscommshow'  => trim($_P['iscommshow']),
    'iscommshow24'   => intval($_P['iscommshow24']),
    'iscommshow_more'   => intval($_P['iscommshow_more']),
    'iscommshowworker'      => intval($_P['iscommshowworker']),
    'isrankrenshu'   => intval($_P['isrankrenshu']),
    'isrankqianshu'   => intval($_P['isrankqianshu']),
    'iscommshowfail'    =>$system['isCommPoints']==1?1:intval($_P['iscommshowfail']),
    'iscommpop'   => intval($_P['iscommpop']),
    'ismycomm'   => intval($_P['ismycomm']),
    'ismycomm_old'   => intval($_P['ismycomm_old']),
    'myredbagstyle'   => intval($_P['myredbagstyle']),
    'commlistimg' => tomedia($_P['commlistimg']),
    'commlistimgbg' => tomedia($_P['commlistimgbg']),
    'commlistbg1'     => empty($_P['commlistbg1'])?'#ffffff':$_P['commlistbg1'],
    'commlistbg2'     => empty($_P['commlistbg2'])?'#fff7f9':$_P['commlistbg2'],
    'commlistbg3'     => empty($_P['commlistbg3'])?'#fff1d0':$_P['commlistbg3'],
    'fenxiaotip'    => $fenxiaotip,
    'ishbicon'   => intval($_P['ishbicon']),
    'fenxiaourl'    => intval($_P['fenxiaourl']),
    'hbtip'    => $hbtip,
    'yaoyiyaotip'    => tomedia($_P['yaoyiyaotip']),
);

if($system['fenxiao06'] == 1){
    $tempconfig['comm61'] = round($_P['comm61'],2);
    $tempconfig['comm62'] = round($_P['comm62'],2);
    $tempconfig['comm61_jiage2'] = round($_P['comm61_jiage2'],2);
    $tempconfig['comm62_jiage2'] = round($_P['comm62_jiage2'],2);
    $tempconfig['comm61_jiage3'] = round($_P['comm61_jiage3'],2);
    $tempconfig['comm62_jiage3'] = round($_P['comm62_jiage3'],2);
    $tempconfig['is_comm6'] = intval($_P['is_comm6']);
    $tempconfig['comm6_mode'] = intval($_P['comm6_mode']);
}
if($system['fenxiao08'] == 1){
    $tempconfig['refresh_other_num'] = intval($_P['refresh_other_num']);
}
if ($system['fenxiao01'] >0){
    $tempconfig['commworker'] = intval($_P['commworker']);
    $tempconfig['intheme'] = intval($_P['intheme']);
    $tempconfig['comm1'] = round($_P['comm11'],2);
    $tempconfig['comm2'] = round($_P['comm12'],2);
    $tempconfig['comm11'] = round($_P['comm11'],2);
    $tempconfig['comm12'] = round($_P['comm12'],2);
    $tempconfig['comm13'] = intval($_P['comm13'])==0?100:intval($_P['comm13']);

    $tempconfig['comm11_2'] = round($_P['comm11_2'],2);
    $tempconfig['comm12_2'] = round($_P['comm12_2'],2);
    $tempconfig['comm13_2'] = intval($_P['comm13_2'])==0?100:intval($_P['comm13_2']);
    $tempconfig['comm11_3'] = round($_P['comm11_3'],2);
    $tempconfig['comm12_3'] = round($_P['comm12_3'],2);

    if ($system['fenxiao01'] >1){
        $tempconfig['isfreeze'] = intval($_P['isfreeze']);
        $tempconfig['comm21'] = round($_P['comm21'],2);
        $tempconfig['comm22'] = round($_P['comm22'],2);
        $tempconfig['comm21_2'] = round($_P['comm21_2'],2);
        $tempconfig['comm22_2'] = round($_P['comm22_2'],2);
        $tempconfig['comm23'] = intval($_P['comm23'])==0?100:intval($_P['comm23']);
    }
    if ($system['fenxiao01'] ==3){
        $tempconfig['comm31'] = round($_P['comm31'],2);
        $tempconfig['comm32'] = round($_P['comm32'],2);
        $tempconfig['comm31_2'] = round($_P['comm31_2'],2);
        $tempconfig['comm32_2'] = round($_P['comm32_2'],2);
        $tempconfig['comm33'] = intval($_P['comm33'])==0?100:intval($_P['comm33']);
    }
    if ($system['jiage'] >0 || $system['bottom_jiage'] == '1' || $system['bottom_jiage'] == '2'){
        $tempconfig['comm11_jiage2'] = round($_P['comm11_jiage2'],2);
        $tempconfig['comm12_jiage2'] = round($_P['comm12_jiage2'],2);
        $tempconfig['comm1_jiage2'] = round($_P['comm11_jiage2'],2);
        $tempconfig['comm2_jiage2'] = round($_P['comm12_jiage2'],2);
        if ($system['fenxiao01'] >1){
            $tempconfig['comm21_jiage2'] = round($_P['comm21_jiage2'],2);
            $tempconfig['comm22_jiage2'] = round($_P['comm22_jiage2'],2);
        }
        if ($system['fenxiao01'] ==3){
            $tempconfig['comm31_jiage2'] = round($_P['comm31_jiage2'],2);
            $tempconfig['comm32_jiage2'] = round($_P['comm32_jiage2'],2);
        }
    }
    if ($system['jiage'] >1 || $system['bottom_jiage'] == '2'){
        $tempconfig['comm11_jiage3'] = round($_P['comm11_jiage3'],2);
        $tempconfig['comm12_jiage3'] = round($_P['comm12_jiage3'],2);
        $tempconfig['comm1_jiage3'] = round($_P['comm11_jiage3'],2);
        $tempconfig['comm2_jiage3'] = round($_P['comm12_jiage3'],2);
        if ($system['fenxiao01'] >1){
            $tempconfig['comm21_jiage3'] = round($_P['comm21_jiage3'],2);
            $tempconfig['comm22_jiage3'] = round($_P['comm22_jiage3'],2);
        }
        if ($system['fenxiao01'] ==3){
            $tempconfig['comm31_jiage3'] = round($_P['comm31_jiage3'],2);
            $tempconfig['comm32_jiage3'] = round($_P['comm32_jiage3'],2);
        }
    }
}
if ($system['fenxiao04'] ==1 || $system['fenxiao05'] ==1 || $system['fenxiao07'] ==1){
    $tempconfig['comm4autored'] = intval($_P['comm4autored']);
    $tempconfig['comm4autoredtime'] = strtotime($_P['comm4autoredtime']);
    $tempconfig['comm4tips'] = trim($_P['comm4tips']);
    $tempconfig['comm4worker'] = intval($_P['comm4worker']);
    $tempconfig['comm4dashuju'] = intval($_P['comm4dashuju']);
    $tempconfig['comm4_time1'] = intval($_P['comm4_time1']);
    $tempconfig['comm4_time2'] = intval($_P['comm4_time2']);


    $tempconfig['comm41'] = round($_P['comm41'],2);
    $tempconfig['comm42'] = round($_P['comm42'],2);
    $tempconfig['comm43'] = intval($_P['comm43'])==0?100:intval($_P['comm43']);
    $tempconfig['comm41_2'] = round($_P['comm41_2'],2);
    $tempconfig['comm42_2'] = round($_P['comm42_2'],2);
    $tempconfig['comm43_2'] = intval($_P['comm43_2'])==0?100:intval($_P['comm43_2']);
    $tempconfig['comm41_3'] = round($_P['comm41_3'],2);
    $tempconfig['comm42_3'] = round($_P['comm42_3'],2);
    $tempconfig['comm4daynum'] = intval($_P['comm4daynum']);
    $tempconfig['comm4sum'] = intval($_P['comm4sum']);
    $tempconfig['comm4type'] = intval($_P['comm4type']);
    $tempconfig['comm4typehb'] = intval($_P['comm4typehb']);
    $tempconfig['comm4typeyg'] = intval($_P['comm4typeyg']);
    $tempconfig['comm4time'] = intval($_P['comm4time']);
    $tempconfig['comm4sharetime'] = intval($_P['comm4sharetime']);
    $tempconfig['comm4yaotime'] = intval($_P['comm4yaotime']);

    $tempconfig['comm4day'] = intval($_P['comm4day']);
    $tempconfig['comm4hour'] = intval($_P['comm4hour']);
    $tempconfig['comm4ip'] = intval($_P['comm4ip']);
    $tempconfig['comm4city'] = formatTxtBySplit(trim($_P['comm4city']));
    $tempconfig['locationtype'] = intval($_P['locationtype']);
    $tempconfig['comm4zhanbi'] = intval($_P['comm4zhanbi']);
    $tempconfig['comm4ispay'] = intval($_P['comm4ispay']);
    $tempconfig['iscommshowwait'] = intval($_P['iscommshowwait']);


    if ($system['fenxiao05'] == '1'){
        $tempconfig['comm5UserLimit'] = intval($_P['comm5UserLimit']);
        $tempconfig['comm51'] = round($_P['comm51'],2);
        $tempconfig['comm52'] = round($_P['comm52'],2);
        $tempconfig['comm53'] = intval($_P['comm53'])==0?100:intval($_P['comm53']);
        $tempconfig['comm51_2'] = round($_P['comm51_2'],2);
        $tempconfig['comm52_2'] = round($_P['comm52_2'],2);
        $tempconfig['comm53_2'] = intval($_P['comm53_2'])==0?100:intval($_P['comm53_2']);
        $tempconfig['comm51_3'] = round($_P['comm51_3'],2);
        $tempconfig['comm52_3'] = round($_P['comm52_3'],2);
        $tempconfig['comm5daynum'] = intval($_P['comm5daynum']);
        $tempconfig['comm5sum'] = intval($_P['comm5sum']);
    }
}
if ($system['autored']==0 || $system['autored']==2  || intval($_P['comm4autored'])==3){
    $tempconfig['hbtixian'] = round($_P['hbtixian'],2);
    $tempconfig['hbtixianmode'] = intval($_P['hbtixianmode']);
    $tempconfig['hbopenfee1'] = round($_P['hbopenfee1'],2);
    $tempconfig['hborderfee1'] = round($_P['hborderfee1'],2);
    $tempconfig['hbopenfee2'] = round($_P['hbopenfee2'],2);
    $tempconfig['hborderfee2'] = round($_P['hborderfee2'],2);
    $tempconfig['hbopenstyle'] = intval($_P['hbopenstyle']);
    $tempconfig['fenxiao06_hpopen'] = intval($_P['fenxiao06_hpopen']);
    $tempconfig['fenxiao01_hpopen'] = intval($_P['fenxiao01_hpopen']);
}
if ($system['fenxiao07'] == '1'){
    $tempconfig['comm7num'] = intval($_P['comm7num']);
    $tempconfig['comm7fee'] = round($_P['comm7fee'],2);
}

if($system['fenxiao09'] == 1){
    $tempconfig['comm91'] = round($_P['comm91'],2);
    $tempconfig['comm92'] = round($_P['comm92'],2);
    $tempconfig['comm91_jiage2'] = round($_P['comm91_jiage2'],2);
    $tempconfig['comm92_jiage2'] = round($_P['comm92_jiage2'],2);
    $tempconfig['comm91_jiage3'] = round($_P['comm91_jiage3'],2);
    $tempconfig['comm92_jiage3'] = round($_P['comm92_jiage3'],2);
}

//代理模式配置
if ($system['selling_agent']==1){
    $tempconfig['comm61_agent'] = round($_P['comm61_agent'],2);
    $tempconfig['comm62_agent'] = round($_P['comm62_agent'],2);
    $tempconfig['comm_agent1'] = round($_P['comm_agent1'],2);
    $tempconfig['comm_agent2'] = round($_P['comm_agent2'],2);
    $tempconfig['is_comm6_agent'] = intval($_P['is_comm6_agent']);
}

if (intval($_P['buttontyle'])==4){
    $tempconfig['commlistimg'] = 'https://css2020.yixuan.net/2020css/jince/hbrank_title.png';
    $tempconfig['commlistbg1'] = '#ffffff';
    $tempconfig['commlistbg2'] = '#fff7f9';
    $tempconfig['commlistbg3'] = '#fff1d0';
    $tempconfig['jince_time_title'] = 1;
}

if ($tempconfig['comm6_mode']==1){
    if (!db_fieldexists(t_team, 'comm61')) {
        $sql = "ALTER TABLE " . tablename(t_team) . " ADD COLUMN `comm61` float(5,2)  NULL DEFAULT 0 ;
            ALTER TABLE " . tablename(t_team) . " ADD COLUMN `comm62` float(5,2)  NULL DEFAULT 0 ;";
        db_query($sql);
    }
}
if ($tempconfig['comm6_mode']==2){
    $comm6_mode2 = array();
    foreach ($_P['comm6_price1'] as $index => $item){
        $limit = $_P['comm6_limit'][$index];
        $price2 = round($_P['comm6_price2'][$index],2);
        $price1 = round($item,2);
        if (empty($comm6_mode2[$limit]) && $price2>=$price1 && $price1>0){
            $comm6_mode2[$limit] = array(
                'limit'=>$limit,
                'price1' => $price1,
                'price2' => $price2,
            );
        }
    }
    ksort($comm6_mode2);
    $tempconfig['comm6_mode2'] = $comm6_mode2;
}

$config =array_merge($config,$tempconfig);